from rest_framework import serializers

from goods.models import Brand, SPUSpecification, SPU, SpecificationOption, GoodsCategory


class BrandSerializer(serializers.ModelSerializer):
    class Meta:
        model = Brand
        fields = ('id', 'name', 'logo', 'first_letter')
        extra_kwargs = {
            'logo': {'required': False}
        }

    def validate_first_letter(self, value):
        value = value.upper()
        return value


class SpecsSerializer(serializers.ModelSerializer):
    spu = serializers.StringRelatedField(label='商品spu')
    spu_id = serializers.IntegerField(label='spu商品id')

    class Meta:
        model = SPUSpecification
        fields = ('id', 'name', 'spu', 'spu_id')

    def validate(self, attrs):
        spu_id = attrs.get('spu_id')
        name = attrs.get('name')
        try:
            spu = SPU.objects.get(id=spu_id)
        except SPU.DoesNotExist:
            raise serializers.ValidationError('spu商品不存在')
        specs = SPUSpecification.objects.filter(spu=spu)
        for spec in specs:
            if name == spec.name:
                raise serializers.ValidationError('规格已存在')

        return attrs


class SpecsSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SPU
        fields = ('id', 'name')


class OptionSerializer(serializers.ModelSerializer):
    spec = serializers.StringRelatedField(label='规格名称')
    spec_id = serializers.IntegerField(label='规格id')

    class Meta:
        model = SpecificationOption
        fields = ('id', 'value', 'spec', 'spec_id')

    def validate(self, attrs):
        spec_id = attrs.get('spec_id')
        value = attrs.get('value')
        try:
            spec = SPUSpecification.objects.filter(id=spec_id)
        except SPUSpecification.DoesNotExist:
            raise serializers.ValidationError('此规格不存在')
        options = SpecificationOption.objects.filter(spec=spec)
        for option in options:
            if value == option.value:
                raise serializers.ValidationError('此规格选项已存在')
        return attrs


class OptionSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SPUSpecification
        fields = ('id', 'name')


class SPUSerializer(serializers.ModelSerializer):
    brand = serializers.StringRelatedField(label='品牌名称')
    brand_id = serializers.IntegerField(label='品牌id')
    category1_id = serializers.IntegerField(label='一级分类id')
    category2_id = serializers.IntegerField(label='二级分类id')
    category3_id = serializers.IntegerField(label='三级分类id')

    class Meta:
        model = SPU
        fields = ('id', 'name', 'brand', 'brand_id', 'category1_id', 'category2_id', 'category3_id', 'sales',
                  'comments', 'desc_detail', 'desc_pack', 'desc_service')
        extra_kwargs = {
            'desc_detail': {'write_only': True, 'required': False},
            'desc_pack': {'write_only': True, 'required': False},
            'desc_service': {'write_only': True, 'required': False},
            'sales': {'read_only': True},
            'comments': {'read_only': True},
        }

    def validate(self, attrs):
        brand_id = attrs.get('brand_id')
        category1_id = attrs.get('category1_id')
        category2_id = attrs.get('category2_id')
        category3_id = attrs.get('category3_id')
        try:
            brand = Brand.objects.get(pk=brand_id)
        except Brand.DoesNotExist:
            raise serializers.ValidationError('品牌不存在')

        try:
            category1 = GoodsCategory.objects.get(pk=category1_id, parent__isnull=True)
        except Brand.DoesNotExist:
            raise serializers.ValidationError('该商品分类不存在')

        try:
            category2 = GoodsCategory.objects.get(pk=category2_id, parent_id=category1_id)
        except Brand.DoesNotExist:
            raise serializers.ValidationError('该商品分类不存在')

        try:
            category3 = GoodsCategory.objects.get(pk=category3_id, parent_id=category2_id)
        except Brand.DoesNotExist:
            raise serializers.ValidationError('该商品分类不存在')

        return attrs


class BrandSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Brand
        fields = ('id', 'name')


class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = ('id', 'name')
